package circuit1;

import java.util.ArrayList;
import java.util.Observable;
/**
 *
 * @author Erik
 */
public class ConsoleController extends Controller {
    
    ArrayList<IElement> elements;
    ArrayList<Wire> wires;
    
    StabilityObserver stabilityObserver;
    
    public ConsoleController() {
        
        Circuit circuit = new Circuit();

        switch(Integer.parseInt(User.Ask("Mit szeretnel csinalni? (0: aramkort kesziteni, 1: 1.aramkort beolvasni, 2: 2.aramkort beolvasni)"))){
            case 0:
                boolean br = false;
                while(!br){
                    switch(Integer.parseInt(User.Ask("Akarsz vezeteket letrehozni? (0: nem, 1: igen): "))){
                        case 1:
                            circuit.addWire(new Wire());
                            Logger.Log("Vezetek letrejott. Sorszama: " + Integer.toString(wires.size()-1));
                            break;
                        case 0:
                            br = true;
                            break;
                        default:
                            br = true;
                            break;
                    }
                }
                br = false;
                while(!br){
                    switch(Integer.parseInt(User.Ask("Akarsz AND kaput letrehozni? (0: nem, 1: igen): "))){
                        case 1:
                            And and = new And();
                            boolean br2 = false;
                            while(!br2){
                                switch(Integer.parseInt(User.Ask("Akarsz raadni bemenetet? (0: nem, 1: igen): "))){
                                    case 1:
                                        and.addInput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg a vezetek sorszamat: "))));
                                        break;
                                    case 0:
                                        br2 = true;
                                    break;
                                    default:
                                        br2 = true;
                                        break;
                                }
                            }
                            and.setOutput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg az output vezetek sorszamat: "))));

                            circuit.addElement(and);
                            Logger.Log("AND kapu letrejott. Elemszama: " + Integer.toString(elements.size()-1));
                            break;
                        case 0:
                            br = true;
                            break;
                        default:
                            br = true;
                            break;
                    }
                }

                br = false;
                while(!br){
                    switch(Integer.parseInt(User.Ask("Akarsz OR kaput letrehozni? (0: nem, 1: igen): "))){
                        case 1:
                            Or or = new Or();
                            boolean br2 = false;
                            while(!br2){
                                switch(Integer.parseInt(User.Ask("Akarsz raadni bemenetet? (0: nem, 1: igen): "))){
                                    case 1:
                                        or.addInput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg a vezetek sorszamat: "))));
                                        break;
                                    case 0:
                                        br2 = true;
                                        break;
                                    default:
                                        br2 = true;
                                        break;
                                }
                            }
                            or.setOutput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg az output vezetek sorszamat: "))));

                            circuit.addElement(or);
                            Logger.Log("OR kapu letrejott. Elemszama: " + Integer.toString(elements.size()-1));
                            break;
                        case 0:
                            br = true;
                            break;
                        default:
                            br = true;
                            break;
                    }
                }
                br = false;
                while(!br){
                    switch(Integer.parseInt(User.Ask("Akarsz Invertert letrehozni? (0: nem, 1: igen): "))){
                        case 1:
                            Inverter inv = new Inverter();
                            inv.addInput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg az input vezetek sorszamat: "))));
                            inv.setOutput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg az output vezetek sorszamat: "))));

                            circuit.addElement(inv);
                            Logger.Log("OR kapu letrejott. Elemszama: " + Integer.toString(elements.size()-1));
                            break;
                        case 0:
                            br = true;
                            break;
                        default:
                            br = true;
                            break;
                    }
                }
                br = false;
                while(!br){
                    switch(Integer.parseInt(User.Ask("Akarsz kapcsolot letrehozni? (0: nem, 1: igen): "))){
                        case 1:
                            Switch sw = new Switch();
                            switch(Integer.parseInt(User.Ask("Add meg a kapcsolo erteket (1/0): "))){
                                case 0:
                                    sw.setSignal(false);
                                    break;
                                case 1:
                                    sw.setSignal(true);
                                    break;
                                default:
                                    sw.setSignal(false);
                                    break;
                            }

                            sw.setOutput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg az output vezetek sorszamat: "))));

                            circuit.addElement(sw);
                            Logger.Log("Kapcsolo letrejott. Elemszama: " + Integer.toString(elements.size()-1));
                            break;
                        case 0:
                            br = true;
                            break;
                        default:
                            br = true;
                            break;
                    }
                }
                br = false;
                while(!br){
                    switch(Integer.parseInt(User.Ask("Akarsz generatort letrehozni? (0: nem, 1: igen): "))){
                        case 1:
                            Generator gen = new Generator();
                            gen.setSignal(User.Ask("Add meg a generator erteket (1/0): "));


                            gen.setOutput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg az output vezetek sorszamat: "))));

                            circuit.addElement(gen);
                            Logger.Log("Generator letrejott. Elemszama: " + Integer.toString(elements.size()-1));
                            break;
                        case 0:
                            br = true;
                            break;
                        default:
                            br = true;
                            break;
                    }
                }
                br = false;
                while(!br){
                    switch(Integer.parseInt(User.Ask("Akarsz LED-et letrehozni? (0: nem, 1: igen): "))){
                        case 1:
                            Led led = new Led();
                            led.setInput(circuit.getWireById(Integer.parseInt(User.Ask("Add meg az input vezetek sorszamat: "))));

                            circuit.addElement(led);
                            Logger.Log("Kapcsolo letrejott. Elemszama: " + Integer.toString(elements.size()-1));
                            break;
                        case 0:
                            br = true;
                            break;
                        default:
                            br = true;
                            break;
                    }
                }
                break;
            case 1:
                
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());

                Generator gen = new Generator();
                gen.setSignal("0111");
                gen.setOutput(circuit.getWireById(0));
                circuit.addElement(gen);

                Switch sw = new Switch();
                sw.setSignal(true);
                sw.setOutput(circuit.getWireById(1));
                circuit.addElement(sw);

                And and = new And();
                and.addInput(circuit.getWireById(0));
                and.addInput(circuit.getWireById(2));
                and.setOutput(circuit.getWireById(3));
                circuit.addElement(and);

                Or or = new Or();
                or.addInput(circuit.getWireById(1));
                or.addInput(circuit.getWireById(4));
                or.setOutput(circuit.getWireById(2));
                circuit.addElement(or);

                Inverter inv = new Inverter();
                inv.addInput(circuit.getWireById(3));
                inv.setOutput(circuit.getWireById(4));
                circuit.addElement(inv);

                Led led = new Led();
                led.setInput(circuit.getWireById(3));
                circuit.addElement(led);

                break;
            case 2:
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());
                circuit.addWire(new Wire());

                sw = new Switch();
                sw.setSignal(false);
                sw.setOutput(circuit.getWireById(0));
                circuit.addElement(sw);

                sw = new Switch();
                sw.setSignal(true);
                sw.setOutput(circuit.getWireById(2));
                circuit.addElement(sw);

                gen = new Generator();
                gen.setSignal("1000");
                gen.setOutput(circuit.getWireById(1));
                circuit.addElement(gen);

                and = new And();
                and.addInput(circuit.getWireById(0));
                and.addInput(circuit.getWireById(1));
                and.setOutput(circuit.getWireById(3));
                circuit.addElement(and);

                or = new Or();
                or.addInput(circuit.getWireById(1));
                or.addInput(circuit.getWireById(2));
                or.setOutput(circuit.getWireById(4));
                circuit.addElement(or);

                or = new Or();
                or.addInput(circuit.getWireById(3));
                or.addInput(circuit.getWireById(4));
                or.setOutput(circuit.getWireById(5));
                circuit.addElement(or);

                inv = new Inverter();
                inv.addInput(circuit.getWireById(5));
                inv.setOutput(circuit.getWireById(6));
                circuit.addElement(inv);

                led = new Led();
                led.setInput(circuit.getWireById(6));
                circuit.addElement(led);
                break;
        }
        
        switch (Integer.parseInt(User.Ask("0: Elem megbokese\n1: Kapcsolo megnyomasa\n2: "
                + "Generator leptetese\n3: Kapu mukodtetese\n4: Osszes elem megbokese\n"))) {
            case 0:
                circuit.getElementById(Integer.parseInt(User.Ask("Irja be az elem sorszamat: "))).prod();
                break;
            case 1:
                circuit.getElementById(Integer.parseInt(User.Ask("Irja be az elem sorszamat: "))).act();
                break;
            case 2:
                circuit.getElementById(Integer.parseInt(User.Ask("Irja be az elem sorszamat: "))).act();
                break;
            case 3:
                circuit.getElementById(Integer.parseInt(User.Ask("Irja be az elem sorszamat: "))).act();
                break;
            case 4:
                boolean br = false;
                while (!br) {
                    for (int i = 0; i < circuit.getElementCount(); i++) {
                        circuit.getElementById(i).prod();
                    }
                    if (Integer.parseInt(User.Ask("Stabil a rendszer? 0:nem, 1:igen ")) == 1) {
                        br = true;
                        break;
                    }
                }
                break;

        }
        
        Logger.Log("<-- Circuit.Circuit()");
        Logger.unindent();
    }
    
    public void update(Observable observable, Object object){
        
    }
    
}
